use ormlite::Model;
use sqlx::{ Sqlite};
use serde::Serialize;
use serde::Deserialize;
use crate::SQLX;
#[derive(Debug, Serialize, Deserialize, Clone, Model)]
pub struct SysUserRole {
    #[ormlite(primary_key)]
    pub user_role_id: Option<i64>,
    pub user_id: Option<i64>,
    pub role_id: Option<i64>,
    pub create_time: Option<String>,
}

impl SysUserRole {
    pub async fn query_by_user(user_id: Option<i64>) -> anyhow::Result<Option<SysUserRole>> {
        let db = SQLX.get().ok_or(anyhow::anyhow!("db error"))?;
        let sys_user_role = sqlx::query_as::<Sqlite, SysUserRole>("select * from sys_user_role where user_id = ?").bind(user_id).fetch_optional(db).await?;
        Ok(sys_user_role)
    }
}
